package com.jzbooks.Interceptor;

import com.jzbooks.utils.ImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author mingPeng
 * @date 2021/4/26 17:09
 * @Description :
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * token 验证器
     */
    @Autowired
    private TokenInterceptor tokenInterceptor;

    /**
     * 图片工具
     */
    @Autowired
    private ImgUtil imgUtil;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(tokenInterceptor);
        if (!CollectionUtils.isEmpty(tokenInterceptor.getIncludePathPatterns())) {
            interceptorRegistration.addPathPatterns(tokenInterceptor.getIncludePathPatterns());
        }
        if (!CollectionUtils.isEmpty(tokenInterceptor.getExcludePathPatterns())) {
            interceptorRegistration.excludePathPatterns(tokenInterceptor.getExcludePathPatterns());
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //用户头像访问地址
        registry.addResourceHandler("img/userImg/**").addResourceLocations(imgUtil.getFileUserImgPath());
        //管理员头像访问地址
        registry.addResourceHandler("img/adminImg/**").addResourceLocations(imgUtil.getFileAdminImgPath());
        //图书图片访问地址
        registry.addResourceHandler("img/bookImg/**").addResourceLocations(imgUtil.getFileBookImgPath());
        //轮播图访问地址
        registry.addResourceHandler("img/carouselImg/**").addResourceLocations(imgUtil.getFileCarouselImgPath());


    }
}
